package ldkxingzhe.l365calendar.view;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import ldkxingzhe.l365calendar.adapter.InfoContainerAdapter;

/**
 * 日历下边的信息容器
 * Created by ke on 16-1-23.
 */
public class InfoContainer extends RelativeLayout{
    @SuppressWarnings("unused")
    private final static String TAG = "InfoContainer";

    private MainLayoutContainer mainLayoutContainer;
    private ListView listView;
    private float touchSlop;
    public InfoContainer(Context context) {
        super(context);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        listView = new ListView(context);
        addView(listView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ArrayAdapter arrayAdapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, new String[]{"1","2",
                "3","4","5","6","7","8","9","10","11","12","13","14","15"});
        listView.setAdapter(arrayAdapter);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mainLayoutContainer = (MainLayoutContainer) getParent();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                touchLastY = ev.getRawY();
                firstTouchY = touchLastY;
                Log.v(TAG, "onInterceptTouchEvent, action down");
                return false;
            case MotionEvent.ACTION_MOVE:
                float dy = ev.getRawY() - touchLastY;
                if(dy < 0 && mainLayoutContainer.monthViewStatus() != MainLayoutContainer.MONTHVIEW_MIN){
                    return true;
                }else if(dy > 0 && listView.getFirstVisiblePosition() == 0){
                    return true;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

    float touchLastY, firstTouchY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                Log.v(TAG, "onTouchEvent, action_down");
                break;
            case MotionEvent.ACTION_MOVE:
                float dy = event.getRawY() - touchLastY;
                mainLayoutContainer.onMonthViewMove((int) dy);
                touchLastY = event.getRawY() + dy - (int)dy;
//                Log.v(TAG, "onTouchEvent, action move, touchLastY is " + touchLastY);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mainLayoutContainer.onMonthViewAnimation(event.getRawY() - firstTouchY);
                Log.v(TAG, "onTouchEvent, pointer up");
                break;
        }
        return true;
    }

    public void setAdapter(InfoContainerAdapter adapter) {
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(adapter);
    }
}
